package com.leo.controller;

import com.leo.bean.Student;
import com.leo.service.IStudentService;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;

/**
 * @author wangss
 */
@FXMLController
@Slf4j
public class StudentController implements Initializable {

    @Autowired
    private IStudentService studentService;

    @FXML
    private TextField sno;

    @FXML
    private TextField name;

    @FXML
    private TextField sex;

    @FXML
    private TextArea studentInfo;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void queryStudentBySno(ActionEvent actionEvent) {
        log.info("文本框中输入的SNO为 [{}]", sno.getText());
        Student student = studentService.queryStudentBySno(sno.getText());
        if (null != student) {
            log.info("根据SNO：[{}] 查询出来的学生信息是 [{}]", sno.getText(), student.toString());
            studentInfo.setText(student.toString());
        } else {
            log.info("根据SNO：[{}] 没有找到学生信息", sno.getText());
            studentInfo.setText("没有找到学生信息");
        }
    }

    public void addStudent(ActionEvent actionEvent) {
        Student student = new Student();
        student.setSno(sno.getText());
        student.setName(name.getText());
        student.setSex(sex.getText());
        studentService.add(student);

        Student student1 = studentService.queryStudentBySno(sno.getText());
        studentInfo.setText(Objects.isNull(student1) ? "没有数据" : student1.toString());
    }

    public void updateStudent(ActionEvent actionEvent) {
        Student student = new Student();
        student.setSno(sno.getText());
        student.setName(name.getText());
        student.setSex(sex.getText());
        studentService.update(student);

        Student student1 = studentService.queryStudentBySno(sno.getText());
        studentInfo.setText(Objects.isNull(student1) ? "没有数据" : student1.toString());
    }

    public void deleteBysno(ActionEvent actionEvent) {
        Student student1 = studentService.queryStudentBySno(sno.getText());
        studentInfo.setText(Objects.isNull(student1) ? "没有数据" : student1.toString());
        studentService.deleteBysno(sno.getText());
        Student student = studentService.queryStudentBySno(sno.getText());
        studentInfo.setText(Objects.isNull(student) ? "没有数据" : student.toString());
    }

    public void queryStudent(ActionEvent actionEvent) {
        List<Student> list = studentService.queryStudent();
        studentInfo.setText(CollectionUtils.isEmpty(list) ? "没有数据" : list.toString());
    }

    public void queryStudentGlobal(ActionEvent actionEvent) {
        List<Student> list = studentService.queryStudentGlobal();
        studentInfo.setText(CollectionUtils.isEmpty(list) ? "没有数据" : list.toString());
    }

    public void addDynamic(ActionEvent actionEvent) {
        Student student = new Student();
        student.setSno(sno.getText());
        student.setName(name.getText());
        student.setSex(sex.getText());
        student.setDataSource("mysql");
        studentService.addDynamic(student);

        Student student1 = studentService.queryStudentBySno(sno.getText());
        studentInfo.setText(Objects.isNull(student1) ? "没有数据" : student1.toString());
    }
}
